package com.linkage.educloud.ah.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.mvp.ABasicPresenter;
import com.zzhou.common.library.util.LogUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * <desc>
 * Created by The Moss on 2018/6/8.
 */

public abstract class BasicTitleFragment extends AbstractFragment{
    protected String TAG = this.getClass().getSimpleName();

    protected abstract int attachLayoutRes();
    Unbinder unBinder;
    protected boolean isFirstVisible = true;
    View rootView;
    Toolbar _mToolbar;
    TextView _rightText;
    ImageView _rightImage;
    FrameLayout _mContentLayout;

    View contentView;
    ABasicPresenter iPresenter;
    protected FragmentManager fragmentManager;
    protected FragmentTransaction fragmentTransaction;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.basic_title_fragment,container,false);
        _mToolbar = rootView.findViewById(R.id._toolbar);
        _rightText = rootView.findViewById(R.id.title_right_text);
        _rightImage = rootView.findViewById(R.id.tittle_right_image);
        _mContentLayout = rootView.findViewById(R.id._content);
        _mToolbar.setTitle("");
        ((AppCompatActivity)getActivity()).setSupportActionBar(_mToolbar);
        setBackBar(true);
        setHasOptionsMenu(true);
        contentView = LayoutInflater.from(getActivity()).inflate(attachLayoutRes(),null);
        _mContentLayout.addView(contentView);
        unBinder = ButterKnife.bind(this,contentView);
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        return rootView;
    }
    protected void initView(ABasicPresenter iaPresenter){
        if (iaPresenter != null) {
            this.iPresenter = iaPresenter;
        }
    };
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getUserVisibleHint()) {
            if (isFirstVisible) {
                isFirstVisible = false;
                onFragmentVisibleChange(true);
            }
        }
    }
    public void setTitle(CharSequence title) {
        _mToolbar.setTitle(title);
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (contentView == null) {
            LogUtils.d(this.getClass().getSimpleName(),"contentView == null");
            return;
        }
        if (isFirstVisible && isVisibleToUser) {
            isFirstVisible = false;
            onFragmentVisibleChange(true);
            return;
        }
    }

    protected void onFragmentVisibleChange(boolean isVisible) {

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unBinder != null) {
            unBinder.unbind();
        }
        if (iPresenter != null) {
            iPresenter.onDestory();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return onOptionsItemSelectedCompat(item);
    }
    public void setBackBar(boolean isShow) {
        ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(isShow);
    }
    protected boolean onOptionsItemSelectedCompat(MenuItem item) {
        return false;
    }

    @Override
    public boolean onBackPressed() {
//        ToastShow.show(getActivity(),"点击了返回");
//        return getChildFragmentManager().popBackStackImmediate();
        return false;
    }
}
